package com.qianwen.wallpaper;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.wallpaper.R;

import java.util.Optional;

public class ConfigActivity extends Activity {
    private static final String TAG = "ConfigActivity";

    private static final String PATH_PREFIX = "/tree/primary:";

    private static final int FILE_REQUEST_CODE = 2001;

    private Button pathSelectBtn;

    private EditText pathView;

    private CheckBox deskCheckBox;

    private CheckBox lockCheckBox;

    private SeekBar seekBar;

    private TextView durationDescView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        setContentView(R.layout.config);
        pathSelectBtn = findViewById(R.id.pathSelect);
        pathSelectBtn.setOnClickListener(this::goFileSelect);
        pathView = findViewById(R.id.pathView);
        pathView.setText(ConfigCache.getPath(this));
        deskCheckBox = findViewById(R.id.desk_check);
        lockCheckBox = findViewById(R.id.lock_check);
        deskCheckBox.setChecked(ConfigCache.getDeskSwitch(this));
        lockCheckBox.setChecked(ConfigCache.getLockSwitch(this));
        deskCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                lockCheckBox.setChecked(true);
            }
            ConfigCache.setDeskSwitch(ConfigActivity.this, isChecked);
        });
        lockCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                deskCheckBox.setChecked(true);
            }
            ConfigCache.setLockSwitch(ConfigActivity.this, isChecked);
        });

        seekBar = findViewById(R.id.duration);
        durationDescView = findViewById(R.id.duration_desc);
        int duration = ConfigCache.getDuration(this);
        seekBar.setProgress(duration);
        durationDescView.setText(String.valueOf(duration));
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (!fromUser) {
                    return;
                }
                ConfigCache.setDuration(ConfigActivity.this, progress);
                durationDescView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    private void goFileSelect(View btn) {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        startActivityForResult(intent, FILE_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FILE_REQUEST_CODE && resultCode == RESULT_OK) {
            String path = Optional.ofNullable(data).map(Intent::getData).map(Uri::getPath)
                    .map(filePath -> filePath.replace(PATH_PREFIX, ""))
                    .orElse(ConfigCache.FILE_PATH);
            Log.i(TAG, "onActivityResult: get file path success: " + path);
            ConfigCache.setPath(this, path);
            pathView.setText(path);
        }
    }
}
